package com.example.webbackend.controller;

import com.example.webbackend.model.Message;
import com.example.webbackend.service.MessageService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;

@RestController
@RequestMapping("/messages")
public class MessageController {

    private final MessageService messageService;

    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }

    // 实时消息订阅(SSE)
    @GetMapping("/subscribe")
    public SseEmitter subscribe(@RequestParam String username) {
        return messageService.subscribe(username);
    }

    // 发送消息
    @PostMapping("/send")
    public Message sendMessage(@RequestBody Message message) {
        return messageService.sendMessage(message);
    }

    // 获取历史消息
    @GetMapping("/history")
    public List<Message> getMessageHistory(@RequestParam Long groupId) {
        return messageService.getMessageHistory(groupId);
    }

    // 获取离线消息
    @GetMapping("/offline")
    public List<Message> getOfflineMessages(@RequestParam String username) {
        return messageService.getOfflineMessages(username);
    }
}